<?php namespace App\Http\Controllers\user\regist;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Models\Users;
use App\Models\UserProfile;
use App\Models\Cellphone;
use App\Models\Email;
use Session;
use Uuid;
use Input;
use Queue;
use App;
use Redirect;
use Log;

class IndexController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		return view('user.regist.index');
	}

	public function addPhoneUser()
	{
		$cellCode = Input::get("cellCode");
		$code = Session::has("cellCode") ? Session::get("cellCode") : '';
		if($cellCode != "" && $cellCode != $code){
			return self::error(self::VALIDATA_CODE_IS_WRONG);
		}
		$phone = trim(Input::get('phone'));
		$password = trim(Input::get('password'));
		$cellphone = new Cellphone();
		if($cellphone->where('cell_phone',$phone)->count()){
			return self::error(self::ACCOUNT_IS_EXIST);
		}
		$phone_reg = '/^1[0-9]{10}$/';
		if(!preg_match($phone_reg,$phone)){
			return '手机号码错误';
		}
		$user = new Users();
		$user->role_id = 1;
		$user->state = 0;
		$user->save();
		$uid = $user->id;

		$profile = new UserProfile;
		$profile->uid = $uid;
		$profile->sex = 1;
		$profile->nickname = "用户" . (10000 + rand() * 10000 + ((int)$uid));
		$profile->contact_way = $phone;
		$profile->save();

		$cellphone->uid = $uid;
		$cellphone->cell_phone = $phone;
		$cellphone->password = $password;
		$cellphone->save();

		$path = base_path() . '/public/images/' . $uid;
		$dir = [env("PROJECT_DIR"), env("TECH_DIR"), env("OTHERS_DIR"), env("DEVICE_DIR")];
		foreach ($dir as $key => $value) {
			$dirPath = $path . '/' . $value;
			is_dir($dirPath) || mkdir($dirPath, 0777, true);
		}
		$user = Users::with(['profile'])->where("id", $uid)->get()[0]->toArray();
		Session::put("user", $user);
		return $this->json([]);
	}

	public function addEmUser()
	{
		$email = Input::get('email');
		$password = Input::get('password');
		$eModel = Email::where("email", $email);
		if($eModel->count())
		{
			return self::error(self::ACCOUNT_IS_EXIST);
		}
		$user = new Users;
		$user->role_id = 1;
		$user->state = 0;
		$user->save();
		$uid = $user->id;

		$profile = new UserProfile;
		$profile->uid = $uid;
		$profile->sex = 1;
		$profile->nickname = "用户" . (10000 + rand() * 10000 + ((int)$uid));
		$profile->save();
		$uuid = Uuid::generate();
		$eModel = new Email;
		$eModel->uid = $uid;
		$eModel->email = $email;
		$eModel->password = $password;
		$eModel->uuid = $uuid;
		$eModel->state = 0;
		$eModel->save();

		$path = base_path() . '/public/images/' . $uid;
		$dir = [env("PROJECT_DIR"), env("TECH_DIR"), env("OTHERS_DIR"), env("DEVICE_DIR")];
		foreach ($dir as $key => $value) {
			$dirPath = $path . '/' . $value;
			is_dir($dirPath) || mkdir($dirPath, 0777, true);
		}

		$url = $_SERVER['HTTP_REFERER'].'/verify_email'.'?u='.$uuid.'&uid='.$uid;
		Queue::push("App\Commands\SendEmail", ["to" => $email, "title" => "来自SendCloud的第一封邮件！", "content" => "你太棒了！你已成功的从SendCloud发送了一封测试邮件，接下来快登录前台去完善账户信息吧！".$url.""]);

		return $this->json([]);
	}

	public function verifyEmail()
	{
		$uuid = Input::get("u");
		$uid = Input::get("uid");
		$model = Email::where("uuid", $uuid)->where("uid", $uid);
		if($model->count())
		{
			$data = [
				"state"	=> 1
			];
			$model->update($data);
			$user = Users::with(['profile'])->where("id", $uid)->get()[0]->toArray();
			Session::put("user", $user);
			return Redirect::to('uc/profile');

		}else{
			return App::abort(404);
		}

	}


	public function verifyCode()
	{
		$phone = Input::get("phone");
		// $seed = [0,1,2,3,4,5,6,7,8,9];
		// $code = '';
		// for ($i=0; $i<6; $i++) {
		// 	$code .= $seed[rand(0,9)];
		// }
		// Session::put("cellCode", $code);
		// $re = Queue::push("App\Commands\SendSMS", ["number" => $phone, "msg" => "【泽大合润投资管理公司】您的验证码是".$code.""]);
		$path = "http://openapi.tanyingjia.com/service?channel=userMobileCodeRequest&platformId=2e2302818a996993c08f2f07c9606e79&loginAccount=" . $phone . "&mobile=" . $phone . "&bizType=reg_mobile";
		$re = json_decode(file_get_contents($path), true);
		Log::info($re);
		return $re;
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
